provider "alicloud" {
  region                = local.region
}

terraform {
  required_version      = ">= 0.12"  
  backend "oss" {
    bucket              = "deeproute-terraform-resources"
    key                 = "stag/services/infra-dtu"
    tablestore_endpoint = "https://terraform-status.cn-shenzhen.ots.aliyuncs.com"
    tablestore_table    = "terraform_statelock_table"    
    region              = "cn-shenzhen"
  }
}

locals {
  vpc          = "vpc-wz9abg6b3gsprwwe2gh0i"
  default_zone = {
    cn-shenzhen-d = ["vsw-wz9yf3t65ta62vzhqrdjx"]
    cn-shenzhen-e = ["vsw-wz98lpzwtjxzs119cxb9x"]
    cn-shenzhen-f = ["vsw-wz92bv1lnv3w55e3ya1xf"]
  }
}

module "rs" {
  source                = "../../../modules/alicloud/random-shuffle"
  default_zone          = local.default_zone
}

module "sg" {
  source                = "../../../modules/alicloud/sg"
  sg_name               = local.fullname
  vpc_id                = local.vpc
  inbound_rules         = {
    "0" = ["0.0.0.0/0", "22/22", "tcp"]
    # "1" = ["0.0.0.0/0", "3306/3306", "tcp"]
    # "2" = ["0.0.0.0/0", "443/443", "tcp"]
    # "3" = ["0.0.0.0/0", "2379/2379", "tcp"]
  }
}

# module "redis" {
#   source                  = "../../../modules/alicloud/redis"
#   zone_id                 = module.rs.zone_id[0]
#   instance_name           = local.fullname
#   instance_type           = "Redis"
#   engine_version          = "5.0"
#   instance_class          = "redis.amber.logic.sharding.1g.2db.0rodb.6proxy.multithread" #redis.amber.logic.sharding.1g.2db.0rodb.6proxy.multithread"  //ref: https://help.aliyun.com/document_detail/26350.html?spm=a2c4g.11186623.6.580.4d2d754eqDUHep
#   vswitch_id              = module.rs.vswitch_id[0]
#   security_group_id       = module.sg.sg_id
#   security_ips            = ["127.0.0.1", "0.0.0.0/0"]  //public access
#   kms_encrypted_password  = "MDBkM2FjZTItNjExMC00ZDVhLWJiNjktMTZkNTBhYzkxNTlkDVJeafR6hRRyBacDKicOkkC4jS53YjZjnEtwJ7sjo8+e2zhCNwWwAgk3iT+A"
#   tags                    = local.tags
# }

# module "rds" {
#   source                      = "../../../modules/alicloud/rds/rds-instance"
#   rds_instance_name           = local.fullname
#   rds_vswitch_id              = [module.rs.vswitch_id[0], module.rs.vswitch_id[0]]
#   rds_zone_id                 = module.rs.zone_id[0]
#   rds_zone_id_slave_a         = "Auto"
#   rds_security_group_ids      = [module.sg.sg_id]
#   tags                        = local.tags
#   rds_security_ips            = ["127.0.0.1", "0.0.0.0/0"]  //public access
#   rds_engine                  = "MySQL"
#   rds_engine_version          = "8.0"
#   rds_instance_type           = "mysql.n2.small.2c" //1h2g ha //https://help.aliyun.com/document_detail/26312.html?spm=a2c4g.11186623.6.570.37b93fa0e1d4lf
#   rds_instance_storage        = "50"
#   rds_instance_charge_type    = "Postpaid"
#   rds_username                = "superadmin"
#   rds_kms_password            = "MDBkM2FjZTItNjExMC00ZDVhLWJiNjktMTZkNTBhYzkxNTlkKpWZ49kk/50NlO7HcOtnNrpOEgtBo2qIQT5A2MrNKABSoIiUE671KQcf2LZxOiT94g=="
#   enable_rds_backup           = true
#   rds_parameters              = [
#     {
#       name                    = "max_connect_errors"
#       value                   = "100000"
#     },    
#     {
#     name                      = "slow_launch_time"
#     value                     = "1"
#     },
#   ]    
# }

# module "rds_database" {
#   source                      = "../../../modules/alicloud/rds/rds-databases"
#   rds_instance_id             = module.rds.rds_id
#   rds_database_name           = "map"
# }

# resource "alicloud_ons_instance" "main" {
#   instance_name               = local.fullname
#   remark                      = "terrform manager rocketmq instance"
#   tags                        = local.tags
# }

# resource "alicloud_ons_group" "main" {
#   group_name                  = "GID-${local.fullname}"
#   instance_id                 = alicloud_ons_instance.main.id
#   remark                      = "terrform manager rocketmq instance"
# }

# resource "alicloud_ons_topic" "main" {
#   topic_name                  = local.fullname
#   instance_id                 = alicloud_ons_instance.main.id
#   message_type                = 0  #"0: normal messages 1: partitionally ordered messages 2: globally ordered messages 4: transactional messages 5: scheduled or delayed messages"
#   perm                        = 6  #	设置该Topic的读写模式。取值说明如下：6：同时支持读写4：禁写2：禁读
#   remark                      = "terraform manager rocketmq instance"
# }

# module "ecs_dtu-etcd-cluster" {
#   source                       = "../../../modules/alicloud/ecs"
#   number_of_instances          = "3"
#   ecs_name                     = "prod-dtu-etcd-cluster"
#   ecs_hostname                 = "drrun-etcd-cluster"
#   ecs_vswitch_id               = module.random.vswitch_id[0]
#   ecs_sg_id                    = [module.sg.sg_id]
#   ecs_key_name                 = "devops-key"
#   ecs_instance_type            = "ecs.c6.xlarge"// "4h48"   
#   ecs_disk_type                = "cloud_essd"         
#   ecs_disk_size                = "200"
#   ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
#   ecs_max_bandwidth_out        = "5"
#   ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
#   ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
#   prepaid_settings             = {
#     "period"             = "1"
#     "period_unit"        = "Month"
#     "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
#     "auto_renew_period"  = "1"
#     "include_data_disks" = "true"
#   }
#   spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
#   role_name                    = "prod-infrastructure-role"
#   tags                         = local.tags
#   userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
#   userdata_VAR_PLAYBOOKS       = [
#     "playbooks/common/load-devops-keys.yml",
#   ]  
# }